﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class UserControl_tabQuoteDetail : System.Web.UI.UserControl
{
    TblQuoteDAO quoteDAO = new TblQuoteDAO(Global.CONNECTION_STRING);
    TblJobsDAO JobDAO = new TblJobsDAO(Global.CONNECTION_STRING);
    TblStatusJobDAO StatusJobDAO = new TblStatusJobDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtdate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtamountQuoted.Attributes.Add("onblur", "textAmount()");
            txtamountQuoted.Attributes.Add("onload", "textAmount()");
            loadQuote();
            LoadQouteLast();
        }
    }

    private void LoadQouteLast()
    {
        string id = Request.QueryString ["id"];
        DataTable dtQuote = quoteDAO.GetQuote(Convert.ToInt32(id));
        foreach (DataRow dr in dtQuote.Rows)
        {
            txtQuoteID.Text = dr["QuoteID"].ToString();
        }
        DataTable dtQuoteLast = quoteDAO.GetQuoteLast(Convert.ToInt32(id), txtQuoteID.Text);
        foreach (DataRow dr in dtQuoteLast.Rows)
        {
            txtfee.Text = dr["InspectionFee"].ToString();
            txtamountQuoted.Text = dr["AmountQuoted"].ToString();
            txtdate.Text = dr["DateQuoted"].ToString();
            txtQuoteID.Text = dr["QuoteID"].ToString();
        }
    }

    private void loadQuote()
    {        
        string id = Request.QueryString ["id"];

        try
        {
            if (id != null)
            {
                DataTable dtQuote = quoteDAO.GetQuote(Convert.ToInt32(id));
                foreach (DataRow dr in dtQuote.Rows)
                {
                    txtQuoteID.Text = dr["QuoteID"].ToString();
                    if (Convert.ToInt32(dr["InspectionFee"]) != 0)
                        rdInspectionFee.Checked = true;
                    else
                        rdAmount.Checked = true;
                }

                grdQuote.DataSource = dtQuote;
                grdQuote.DataBind();
            }
            else
            {
                txtfee.Text = string.Empty;
                txtamountQuoted.Text = string.Empty;
                txtdate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            }
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
    }
    protected void btsave_Click(object sender, EventArgs e)
    {
        bool flag = false;
        int id = Global.ConvertToInt(Request.QueryString["id"].ToString());
        try
        {
            
            TblQuote tblQuote = new TblQuote();
            tblQuote.JobID = id;
            if (rdInspectionFee.Checked == true)
            {
                tblQuote.InspectionFee = Convert.ToDecimal(txtfee.Text);
                tblQuote.AmountQuoted = 0;
            }
            if (rdAmount.Checked == true)
            {
                tblQuote.InspectionFee = 0;
                tblQuote.AmountQuoted = Convert.ToDecimal(txtamountQuoted.Text);
            }
            
            tblQuote.DateQuoted = DateTime.Now;
            tblQuote.QuoteStatus = drAmount.SelectedItem.Text;
            tblQuote.Notes = txtNote.Text;
            tblQuote.OutCome = "";

            if (txtQuoteID.Text == "")
            {
                quoteDAO.InsTblQuote(tblQuote, 1);
            }
            else
            {
                tblQuote.QuoteID = Convert.ToInt32(txtQuoteID.Text);
                quoteDAO.InsTblQuote(tblQuote, 2);

            }

            TblJobs Jobs = new TblJobs();

            Jobs.JobID = id;
            if (drAmount.SelectedValue == "1")
            {
                Jobs.QuotationStatus = 1;
            }
            if (drAmount.SelectedValue == "2")
            {
                Jobs.QuotationStatus = 2;
            }
            if (drAmount.SelectedValue == "3")
            {
                Jobs.QuotationStatus = 3;
            }
            if (drAmount.SelectedValue == "4")
            {
                Jobs.QuotationStatus = 4;
            }

            JobDAO.UpdateQuoted(Jobs.QuotationStatus, id);

            TblJobStatus JobStatus = new TblJobStatus();
            JobStatus.JobID = id;
            JobStatus.StatusDate = DateTime.Now;
            JobStatus.StatusJobs = tblQuote.QuoteStatus;
            JobStatus.Notes = tblQuote.Notes;

            StatusJobDAO.JobStatusIns(JobStatus);

            flag = true;

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (flag)
        {
            loadQuote();
            //Response.Redirect("~/Booking.aspx?id=" + id);
            //Response.Redirect("~/Booking.aspx?id=" + id);
            ScriptManager.RegisterStartupScript(btsave, btsave.GetType(), "ScriptLoginSuccess", "UpdateParentsLogin();", true);
        }


    }
    protected void grdQuote_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdQuote.PageIndex = e.NewPageIndex;
        loadQuote();
    }
}
